<?php

namespace app\controller\admin\cnf;

use app\bll\ComponentExtend as BllComponentExtend;
use app\bll\Hook as BllHook;
use app\bll\MsgChannel as BllMsgChannel;
use app\bll\MsgTemplate as BllMsgTemplate;
use core\controller\AdminBase;
use Exception;
use support\View;

class Message extends AdminBase
{
    public function channels()
    {
        $bllMsgChannel = new BllMsgChannel();
        $channels      = $bllMsgChannel->paginate();
        View::assign('channels', $channels);
        return $this->view();
    }

    public function addChannel()
    {
        if (is_post()) {

            try {
                $bllMsgChannel = new BllMsgChannel();
                $bllMsgChannel->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllComponentExtend = new BllComponentExtend();
            $extends            = $bllComponentExtend->cascader(groupid: 1);
            View::assign('extends', $extends);
            return $this->view();
        }
    }

    public function editChannel()
    {
        $id            = request()->get('id');
        $bllMsgChannel = new BllMsgChannel();
        if (is_post()) {
            try {
                $bllMsgChannel = new BllMsgChannel();
                $bllMsgChannel->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $channelinfo        = $bllMsgChannel->find($id);
            $bllComponentExtend = new BllComponentExtend();
            $extends            = $bllComponentExtend->cascader(groupid: 1);
            View::assign('channelinfo', $channelinfo);
            View::assign('extends', $extends);
            return $this->view();
        }
    }

    /**
     * 删除频道
     *
     * @return void
     */
    public function delchannel()
    {
        try {
            $bllMsgChannel = new BllMsgChannel();
            $bllMsgChannel->delete(request()->get('id'));
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    public function templates()
    {
        $bllMsgTemplate = new BllMsgTemplate();
        $templates      = $bllMsgTemplate->paginate();

        $bllMsgChannel = new BllMsgChannel();
        $channels      = $bllMsgChannel->select();

        View::assign('channels', array_column($channels, null, 'id'));
        View::assign('templates', $templates);
        return $this->view();
    }

    /**
     * 创建消息模板
     *
     * @return mixed
     */
    public function addTemplate()
    {
        if (is_post()) {
            try {
                $bllMsgTemplate = new BllMsgTemplate();
                $bllMsgTemplate->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            if (request()->get('type') == 'getHookParam') {
                $bllHook = new BllHook();
                $hookinfo = $bllHook->findByIdent(request()->get('eventhookident'));
                return $this->success($hookinfo);
            }

            //获取通道
            $bllMsgChannel = new BllMsgChannel();
            $channels = $bllMsgChannel->select();



            //钩子
            $bllHook =  new BllHook();
            $hooks = $bllHook->cascader();

            View::assign('channels', $channels);
            View::assign('hooks', $hooks);
            return $this->view();
        }
    }

    /**
     * 编辑消息模板
     *
     * @return mixed
     */
    public function editTemplate()
    {
        $id             = request()->get('id');
        $bllMsgTemplate = new BllMsgTemplate();
        if (is_post()) {
            try {
                $bllMsgTemplate->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            if (request()->get('type') == 'getHookParam') {
                $bllHook = new BllHook();
                $hookinfo = $bllHook->findByIdent(request()->get('eventhookident'));
                return $this->success($hookinfo);
            }

            $info = $bllMsgTemplate->find($id);

            //获取通道
            $bllMsgChannel = new BllMsgChannel();
            $channels = $bllMsgChannel->select();

            //钩子
            $bllHook =  new BllHook();
            $hooks = $bllHook->cascader();

            View::assign('channels', $channels);
            View::assign('hooks', $hooks);
            View::assign('info', $info);
            return $this->view();
        }
    }

    /**
     * 删除消息模板
     *
     * @return void
     */
    public function delTemplate()
    {
        try {
            $bllMsgTemplate = new BllMsgTemplate();
            $bllMsgTemplate->delete(request()->get('id'));
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
